import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    delBtnShow: true,
    array: [],
    index: null,
    list: [],
    prevPageForm: {},
  },
  onLoad() {
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    this.data.prevPageForm = prevPage.data.form
    if (this.data.prevPageForm.stepCode == "") {
      this.getListData()
    } else {
      this.mateDataPropertyQuery()
    }
  },

  getListData() {
    let that = this
    let form = this.data.prevPageForm
    let arr = []
    for (let i in form.columns) {
      // let num = form.columns.length - i
      let find = that.data.array.findIndex(n => {
        return n.propertyCode == form.columns[i]
      })
      // let obj = {
      //   columnIndex: find,
      //   columns: form.columns[i],
      //   columnData: form.columnData[i],
      //   title: '工艺参数' + num
      // }
      let obj = {
        columnIndex: find,
        columns: form.columns[i],
        columnData: form.columnData[i],
        title: '工艺参数'
      }
      arr.push(obj)
    }
    that.setData({
      list: arr
    })
  },

  mateDataPropertyQuery() {
    let that = this
    $http(apiSetting.mateDataPropertyQuery, {
      stepCode: that.data.prevPageForm.stepCode,
      dataType: "TechnocsParam", //定值 工艺 - TechnocsParam 质检指标 - QualityControl
    }).then((data) => {
      that.setData({
        array: data.result.propertyDTOList
      })
      that.getListData()
    })
  },

  addList() {
    let obj = {
      columns: '',
      columnData: null,
      title: '工艺参数'
    }
    // this.data.list.unshift(obj)
    this.data.list.push(obj)
    this.setData({
      list: this.data.list
    })
  },

  delList(e) {
    let ind = e.target.dataset.index
    this.data.list.splice(ind, 1)
    this.setData({
      list: this.data.list
    })
    this.update()
  },

  bindObjPickerChange(e) {
    let ind = e.target.dataset.index
    let value = e.detail.value
    let find = this.data.list.findIndex(n => {
      return n.columns == this.data.array[value].propertyCode
    })
    if (find != -1) {
      dd.alert({ content: "参数不能重复选择" });
      return
    }
    this.data.list[ind].columns = this.data.array[value].propertyCode
    this.data.list[ind].columnIndex = value
    this.setData({
      list: this.data.list
    });
    this.update()
  },

  onInputChange(e) {
    let ind = e.target.dataset.index
    let value = e.detail.value
    this.data.list[ind].columnData = value
    this.update()
  },

  update() {
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    let columns = []
    let columnData = []
    for (let i in this.data.list) {
      columns.push(this.data.list[i].columns)
      columnData.push(this.data.list[i].columnData)
    }
    // for (let i = columnData.length; i < this.data.array.length; i++) {
    //   columnData[i] = null
    // }
    for (let i in this.data.array) {
      let findInd = columns.findIndex(n => {
        return n == this.data.array[i].propertyCode
      })
      if (findInd == -1) {
        columns.push(this.data.array[i].propertyCode)
        columnData.push(null)
      }
    }
    prevPage.data.form.columns = columns
    prevPage.data.form.columnData = columnData
    prevPage.data.form.dataId = this.data.array[0].dataId
  }
});
